package com.finance.cooperate.feature.reader;


import com.finance.cooperate.common.constant.Constant;
import com.finance.cooperate.dao.local.cache.CrcCache;
import com.finance.cooperate.feature.base.ReaderImpl;
import com.finance.cooperate.feature.core.feature.FeatureDefinition;

import java.util.Map;

/**
 * @ClassName CrcReader
 * @Description 征信数据抽取
 * @Author shen
 * @Date 2022/5/26 22:44
 * @Modify ...
 */
public class CrcReader extends ReaderImpl {
    @Override
    public Map<String, String> read(String userId) {

        Integer score = crcScoresService.getScore(userId);
        super.put(FeatureDefinition.f_crc_score, score);

        Map<String, Object> consultasMap = crcConsultasService.getInfo(userId);

        CrcCache.getTimeSplitsV1().forEach((tk, tv) -> {

            CrcCache.getGrantMapValues().forEach(gv -> {

                CrcCache.getCreditTypeMapValues().forEach(cv -> {

                    CrcCache.getCurrencyMapValues().forEach(crv -> {

                        super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_credit_%s_currency_%s_amount_sum", tk, gv, cv, crv)
                                , consultasMap.getOrDefault(
                                        String.format("consultas_%s_grant_%d_credit_%s_currency_%s_amount_sum", tk, gv, cv, crv)
                                        , Constant.MISSING_VALUE)
                        );

                        super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_credit_%s_currency_%s_count", tk, gv, cv, crv)
                                , consultasMap.getOrDefault(
                                        String.format("consultas_%s_grant_%d_credit_%s_currency_%s_count", tk, gv, cv, crv)
                                        , Constant.MISSING_VALUE)
                        );

                        super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_credit_%s_currency_%s_amount_max", tk, gv, cv, crv)
                                , consultasMap.getOrDefault(
                                        String.format("consultas_%s_grant_%d_credit_%s_currency_%s_amount_max", tk, gv, cv, crv)
                                        , Constant.MISSING_VALUE)
                        );

                        super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_credit_%s_currency_%s_amount_min", tk, gv, cv, crv)
                                , consultasMap.getOrDefault(
                                        String.format("consultas_%s_grant_%d_credit_%s_currency_%s_amount_min", tk, gv, cv, crv)
                                        , Constant.MISSING_VALUE)
                        );

                        super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_credit_%s_currency_%s_amount_avg", tk, gv, cv, crv)
                                , consultasMap.getOrDefault(
                                        String.format("consultas_%s_grant_%d_credit_%s_currency_%s_amount_avg", tk, gv, cv, crv)
                                        , Constant.MISSING_VALUE)
                        );

                    });

                });

                CrcCache.getCurrencyMapValues().forEach(crv -> {

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_currency_%s_amount_sum", tk, gv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_grant_%d_currency_%s_amount_sum", tk, gv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_currency_%s_count", tk, gv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_grant_%d_currency_%s_count", tk, gv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_currency_%s_amount_max", tk, gv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_grant_%d_currency_%s_amount_max", tk, gv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_currency_%s_amount_min", tk, gv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_grant_%d_currency_%s_amount_min", tk, gv, crv)
                                    , Constant.MISSING_VALUE)
                    );


                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_%d_currency_%s_amount_avg", tk, gv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_grant_%d_currency_%s_amount_avg", tk, gv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                });

            });


            CrcCache.getCreditTypeMapValues().forEach(cv -> {

                CrcCache.getCurrencyMapValues().forEach(crv -> {

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_credit_%s_currency_%s_amount_sum", tk, cv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_credit_%s_currency_%s_amount_sum", tk, cv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_credit_%s_currency_%s_count", tk, cv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_credit_%s_currency_%s_count", tk, cv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_credit_%s_currency_%s_amount_max", tk, cv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_credit_%s_currency_%s_amount_max", tk, cv, crv)
                                    , Constant.MISSING_VALUE)
                    );

                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_credit_%s_currency_%s_amount_min", tk, cv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_credit_%s_currency_%s_amount_min", tk, cv, crv)
                                    , Constant.MISSING_VALUE)
                    );



                    super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_credit_%s_currency_%s_amount_avg", tk, cv, crv)
                            , consultasMap.getOrDefault(
                                    String.format("consultas_%s_credit_%s_currency_%s_amount_avg", tk, cv, crv)
                                    , Constant.MISSING_VALUE)
                    );




                });

            });


            CrcCache.getCurrencyMapValues().forEach(crv -> {

                super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_currency_%s_amount_sum", tk, crv)
                        , consultasMap.getOrDefault(
                                String.format("consultas_%s_currency_%s_amount_sum", tk, crv)
                                , Constant.MISSING_VALUE)
                );

                super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_currency_%s_count", tk, crv)
                        , consultasMap.getOrDefault(
                                String.format("consultas_%s_currency_%s_count", tk, crv)
                                , Constant.MISSING_VALUE)
                );

                super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_currency_%s_amount_max", tk, crv)
                        , consultasMap.getOrDefault(
                                String.format("consultas_%s_currency_%s_amount_max", tk, crv)
                                , Constant.MISSING_VALUE)
                );

                super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_currency_%s_amount_min", tk, crv)
                        , consultasMap.getOrDefault(
                                String.format("consultas_%s_currency_%s_amount_min", tk, crv)
                                , Constant.MISSING_VALUE)
                );

                super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_currency_%s_amount_avg", tk, crv)
                        , consultasMap.getOrDefault(
                                String.format("consultas_%s_currency_%s_amount_avg", tk, crv)
                                , Constant.MISSING_VALUE)
                );


            });

            super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_grant_size", tk)
                    , consultasMap.getOrDefault(
                            String.format("consultas_%s_grant_size", tk)
                            , Constant.MISSING_VALUE)
            );

            super.put(FeatureDefinition.f_crc_consultas_.getValue() + String.format("%s_credit_type_size", tk)
                    , consultasMap.getOrDefault(
                            String.format("consultas_%s_credit_type_size", tk)
                            , Constant.MISSING_VALUE)
            );

        });



        return super.getResult();
    }
}
